//
//  TableViewController.m
//  调用摄像头
//
//  Created by administrator on 12-9-12.
//  Copyright (c) 2012年 administrator. All rights reserved.
//

#import "TableViewController.h"
#import "ImageShowViewController.h"
//#import "AppDelegate.h"

@interface TableViewController ()

@property (strong, nonatomic) ImageShowViewController *childController;

@end

@implementation TableViewController
@synthesize imageList;
@synthesize childController;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void) LoadData
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"documentsDirectory%@",documentsDirectory);
    NSFileManager *fileManage = [NSFileManager defaultManager];
    NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"Image"];
    
    //遍历沙盒中指定文件夹的文件
    NSArray *file = [fileManage subpathsOfDirectoryAtPath: myDirectory error:nil];  //方法1
    
    
    
//    if (file !=nil) {
//        [file removeObjectAtIndex:0];
//    }
    if ([file containsObject:@".DS_Store"]) {
        [file removeObjectAtIndex:0];
    }
    
    
    NSLog(@"%@",file);
    //    NSArray *files = [fileManage subpathsAtPath: myDirectory ];       //方法2
    //    NSLog(@"%@",files);
    
    // NSArray *array = [[NSArray alloc] initWithObjects:@"1", @"2", @"3",nil];
    self.imageList = file;
}

- (void)viewDidLoad
{
        
    [self LoadData];
    
    [super viewDidLoad];
    
    
    
    
    
    
    

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}


- (void)viewWillAppear:(BOOL)animated
{

    [self LoadData];

    
    [self.tableView reloadData];
    [super viewWillAppear:animated];
  //  NSLog(@"asdfgh");
    
}



- (void)viewDidUnload
{
    self.imageList=nil;
    self.childController=nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [imageList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ImageTableViewCell = @"ImageTableViewCell";
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier: ImageTableViewCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier: ImageTableViewCell];
    }
    NSUInteger row = [indexPath row];
    NSString *imageTitle = [imageList objectAtIndex:row];
    //这里设置每一行显示的文本为所对应的View Controller的标题
    cell.textLabel.text = imageTitle;
    //accessoryType就表示每行右边的图标
    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //箭头
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (childController == nil) {
        childController = [[ImageShowViewController alloc]
                           initWithNibName:@"ImageShowViewController" bundle:nil];
    }
    NSUInteger row = [indexPath row];
    NSString *selectedImage = [imageList objectAtIndex:row];
    NSString *detailMessage = [[NSString alloc]
                               initWithFormat:@"%@", selectedImage];
    
   // NSLog(@"%@",detailMessage);
    
    
    childController.message = detailMessage;
    childController.title = selectedImage;
    [self.navigationController pushViewController:childController animated:YES];
    
}

//- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
//    if (childController == nil) {
//        childController = [[ImageShowViewController alloc]
//                           initWithNibName:@"ImageShowViewController" bundle:nil];
//    }
//    NSUInteger row = [indexPath row];
//    NSString *selectedImage = [imageList objectAtIndex:row];
//    NSString *detailMessage = [[NSString alloc]
//                               initWithFormat:@"你选择了电影：%@.", selectedImage];
//    childController.message = detailMessage;
//    childController.title = selectedImage;
//    [self.navigationController pushViewController:childController animated:YES];
//}



//删除列表中的某个值
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    ///删除照片
    
    NSUInteger row = [indexPath row];
    NSString *selectedImage = [imageList objectAtIndex:row];
    NSString *removeimage = [[NSString alloc]
                             initWithFormat:@"Image/%@", selectedImage];
    //NSString *removeimage=[NSString stringWithFormat:@"Image/%@",childController.message];
    NSLog(@"%@",removeimage);
    
    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    
    //文件名
    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:removeimage];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
    if (!blHave) {
        NSLog(@"no  have");
        return ;
    }else {
        NSLog(@" have");
        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
        if (blDele) {
            NSLog(@"dele success");
        }else {
            NSLog(@"dele fail");
        }
        
    }
    
    
   
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		
		[self.imageList removeObjectAtIndex:indexPath.row];
		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
						 withRowAnimation:UITableViewRowAnimationFade];
	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}

@end
